/**
 * Copyright 2009 University of Washington This code is released under the GPL version 3 - with a copy of the license
 * included
 * 
 * @author Maya Hao Li
 * 
 */
package seedpod.webapp.view.seedpodwidgets;

/**
 * this widget supports a drop down of options
 */
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import seedpod.exceptions.InvalidWidgetException;
import seedpod.exceptions.SeedpodException;
import seedpod.kb2db.ModelMap;
import seedpod.model.AVPair;
import seedpod.model.ProtegeValueType;
import seedpod.model.SeedpodModel.RdbCls.FormWidget;
import seedpod.model.SeedpodModel.RdbCls.ViewWidget;
import seedpod.model.rdb.MetaRdbSlot;
import seedpod.model.rdb.RdbValueType;
import seedpod.rdb.PMFactory;
import seedpod.rdb.PManager;
import seedpod.rdb.SqlUtil;
import seedpod.webapp.view.htmlwidget.SelectInput;

public class ComboBox extends LimsWidget {

	private String[] _choices;

	public ComboBox() {
	// TODO Auto-generated constructor stub
	}

	public ComboBox(AVPair avpair) {
		super( avpair );
		init();
	}

	@Override
	public String getData() {
		String strVal = "";
		if ( _avPair != null ) try {
			strVal = (String)super.getData();
		} catch (ClassCastException ce) {
			strVal = "";
		}
		return strVal;
	}

	@Override
	public FormWidget getFormWidgetType() {
		return FormWidget.COMBO;
	}

	@Override
	public ViewWidget getViewWidgetType() {
		return null;
	}

	private void init() {
		String allSymbols = getAttribute().getAllowedValues();
		_choices = allSymbols.split( MetaRdbSlot.VALUE_DELIMITER );
		if ( _choices == null )
			if ( getAttribute().isRelationship() ) {
				String tableName = getAttribute().getAllowedCls();
				ModelMap.getInstance().getClsMap( tableName );
				String sql = "SELECT " + SqlUtil.QQ( "ID" ) + " FROM " + tableName;

				_choices = new String[1];
				_choices[0] = sql;

			} else {
				String tableName = getAttribute().getRdbClsName();
				String sqlChoices = "SELECT DISTINCT " + SqlUtil.QQ( getAttribute().getName() ) + " as c0 FROM " + SqlUtil.QQ( tableName );

				try {
					PManager dbManager = PMFactory.getInstance().createManager();
					ResultSet results = dbManager.getRdbConnection().executeQuery( sqlChoices );

					Vector<String> choiceVector = new Vector<String>();
					while (results.next())
						choiceVector.add( results.getString( "c0" ) );

					_choices = new String[choiceVector.size()];
					for ( int c = 0; c < choiceVector.size(); c++ )
						_choices[c] = choiceVector.get( c );
				} catch (SQLException sqle) {
					_choices = new String[1];
					_choices[0] = "<no choices available>";
				} catch (SeedpodException e) {
					_choices = new String[1];
					_choices[0] = "<no choices available>";
				}
			}
	}

	@Override
	public String render() {
		SelectInput seInput = new SelectInput( getAttribute().getName(), getData(), _choices );
		seInput.setSelectedIndex( 0 );
		return seInput.render();
	}

	@Override
	public void validateWidget() throws InvalidWidgetException {
		boolean valid = getAttribute().getRdbValueType() == RdbValueType.VARCHAR || getAttribute().getProtegeValueType().equals(
				ProtegeValueType.Symbol );
		if ( !valid )
			throw new InvalidWidgetException( "Combox box requres either string or symbol data value types." );
	}
}
